/*
* 让日期区间更友好！
把常见的日期格式如：YYYY-MM-DD 转换成一种更易读的格式。
易读格式应该是用月份名称代替月份数字，用序数词代替数字来表示天 (1st 代替 1).
记住不要显示那些可以被推测出来的信息: 如果一个日期区间里结束日期与开始日期相差小于一年，则结束日期就不用写年份了；在这种情况下，如果月份开始和结束日期如果在同一个月，则结束日期月份也不用写了。
另外, 如果开始日期年份是当前年份，且结束日期与开始日期小于一年，则开始日期的年份也不用写。
例如:
包含当前年份和相同月份的时候，makeFriendlyDates(["2017-01-02", "2017-01-05"]) 应该返回 ["January 2nd","5th"]
不包含当前年份，makeFriendlyDates(["2003-08-15", "2009-09-21"]) 应该返回 ["August 15th, 2003", "September 21st, 2009"]。
请考虑清楚所有可能出现的情况，包括传入的日期区间是否合理。对于不合理的日期区间，直接返回 undefined 即可
*/

function makeFriendlyDates(arr) {

    var months = {     //这里先弄个月份对象
        "01":"January",
        "02":"February",
        "03":"March",
        "04":"April",
        "05":"May",
        "06":"June",
        "07":"July",
        "08":"August",
        "09":"September",
        "10":"October",
        "11":"November",
        "12":"December"
    };
    var date=[],dateArr=[];
    var dateString = "";
    //把arr分割成二维数组
    for(var i=0;i<arr.length;i++){
        date[i] = arr[i];
        date[i] = date[i].split("-");
    }

    //去除不正确日期
    if(date[0][0]>date[1][0])return undefined;
    if(date[0][0]==date[1][0]&&date[0][1]>date[1][1])return undefined;
    if(date[0][0]==date[1][0]&&date[0][1]==date[1][1]&&date[0][2]>date[1][2]) return undefined;


    var minus = date[1][0] - date[0][0] ;
    //如果年月日都相等
    if(date[0][0]==date[1][0]&&date[0][1]==date[1][1]&&date[0][2]==date[1][2]){
        dateString = months[date[0][1]]+" "+tailOfDay(date[0][2])+", "+date[0][0];
        dateArr.push(dateString) ;
        return dateArr;
    }
    //如果年月相等
    else if(date[0][0]==date[1][0]&&date[0][1]==date[1][1]){
        dateString = months[date[0][1]]+" "+tailOfDay(date[0][2]);
        dateArr.push(dateString,tailOfDay(date[1][2]));
        return dateArr;
    }
    //如果只有年相等
    else if(date[0][0]==date[1][0]){
        dateString = months[date[0][1]]+" "+tailOfDay(date[0][2]);
        dateArr.push(dateString);
        dateString = months[date[1][1]]+" "+tailOfDay(date[1][2]);
        dateArr.push(dateString);
        return dateArr;
    }
    else{
        //如果不是当前年份且时间间隔小于一年
        if((date[0][0]!=2017&&date[0][1]>date[1][1])||(date[0][1]==date[1][1]&&date[0][2]>date[1][2])&&minus<=1){
            dateString = months[date[0][1]]+" "+tailOfDay(date[0][2])+", "+date[0][0];
            dateArr.push(dateString) ;
            dateString = months[date[1][1]]+" "+tailOfDay(date[1][2]);
            dateArr.push(dateString);
            return dateArr;
        }
        //如果是当前年份且时间间隔小于一年
        else if(date[0][0]==2017 && date[0][1]>date[1][1] && date[1][0]==2018){
            dateString = months[date[0][1]]+" "+tailOfDay(date[0][2]);
            dateArr.push(dateString);
            dateString = months[date[1][1]]+" "+tailOfDay(date[1][2]);
            dateArr.push(dateString);
            return dateArr;
        }
        //其他
        else{
            dateString = months[date[0][1]]+" "+tailOfDay(date[0][2])+", "+date[0][0];
            dateArr.push(dateString) ;
            dateString = months[date[1][1]]+" "+tailOfDay(date[1][2])+", "+date[1][0];
            dateArr.push(dateString);
            return dateArr;
        }
    }
}
//把日期转换成对应的简便字符串
function tailOfDay(day){
    day = parseInt(day);
    if(day==1||day==21||day==31){
        day = day.toString() + "st";
        return day;
    }
    else if(day==2||day==22){
        day = day.toString() + "nd";
        return day;
    }
    else if(day==3||day==23){
        day = day.toString() + "rd";
        return day;
    }
    else{
        day = day.toString() + "th";
        return day;
    }
}

makeFriendlyDates(["2017-07-12", "2018-06-13"]);
//makeFriendlyDates(["2016-12-01", "2017-02-03"]);
//makeFriendlyDates(["2016-12-01", "2018-02-03"]);
//makeFriendlyDates(["2017-03-01", "2017-05-05"]);
//makeFriendlyDates(["2018-01-13", "2018-01-13"]);
//makeFriendlyDates(["2022-09-05", "2023-09-04"]);
//makeFriendlyDates(["2022-09-05", "2023-09-05"])